from flask import Blueprint, jsonify, request, session
from flask import render_template
from bt import Bt_api
from decorator import login_required
from flask import current_app
blueprint = Blueprint('/GetRewriteList', __name__, url_prefix='/GetRewriteList')


# 获取伪静态列表
@blueprint.route("/", endpoint='/', methods=['GET', 'POST'])
@login_required
def GetRewriteList():
    
    if request.method == 'POST':
        # 实例化宝塔API对象
        bt=Bt_api(session.get('bt_url') or '',session.get('bt_key') or '')
        data = bt.GetRewriteList(session.get('bt_web') or '')
        return jsonify(data['rewrite'])
    cdn=current_app.config['CDN']
    
 
    return render_template('GetRewriteList.html',cdn=cdn)

# 修改伪静态页面
@blueprint.route("/GetFileBody", endpoint='GetFileBody', methods=['GET', 'POST'])
@login_required
def GetFileBody():
    # 实例化宝塔API对象
    bt=Bt_api(session.get('bt_url') or '',session.get('bt_key') or '')
    
    if request.method == 'POST':
        path = request.form.get('path') or ''
       
        if path == '/':
        
            data = bt.GetFileBody('/www/server/panel/vhost/rewrite/'+session.get('bt_web','')+'.conf')
           
            return jsonify(data)
        
        data = bt.GetFileBody('/www/server/panel/rewrite/nginx/'+path+'.conf')
       
        return jsonify(data)
    

# 保存伪静态
@blueprint.route("/SaveFileBody", endpoint='SaveFileBody', methods=['GET', 'POST'])
@login_required
def SaveFileBody():
    # 实例化宝塔API对象
    bt=Bt_api(session.get('bt_url') or '',session.get('bt_key') or '')
    if request.method == 'POST':
        data = request.form.get('data') or ''
        result = bt.SaveFileBody(data,'/www/server/panel/vhost/rewrite/'+session.get('bt_web','')+'.conf')
        return jsonify(result)

